﻿using System;
using System.Collections.Generic;

using E.BLL;
using E.Model;
using E.Web.UI.Page;
using E.Web.Common;

using X.Common;

namespace E.Web.Teacher.internals.publics.statistics
{
    public partial class Export2Xls2 : TeacherBasePage
    {
        protected override void ShowPage()
        {
            if (ispost)
            {
                string request_id = XRequest.GetString("arrv");
                if (request_id != "")
                {
                    string[] idarr;
                    if (request_id.IndexOf(',') != -1)
                    {
                        idarr = request_id.Split(',');
                    }
                    else
                        idarr = new string[] { request_id };

                    IList<CourseSurveyReportModel> list = (new SurveyBLL()).GetPublicCourseAnalysisInfo2(idarr);

                    WebCommon.ExportExcelData(TableTemplate(list));
                }
            }
        }

        private System.Text.StringBuilder TableTemplate(IList<CourseSurveyReportModel> list)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder("");

            if (list.Count > 0)
            {
                foreach (CourseSurveyReportModel model in list)
                {
                    sb.Append("<table border=\"1\" cellpadding=\"6\" cellspacing=\"2\" style=\"width:600px; font:12px '宋体'; border-collapse:collapse; border-color:#ccc; border-width:1px;\">\r\n");
                    sb.Append("  <tr>\r\n");
                    sb.Append("    <td colspan=\"2\" style=\"font-weight:bolder; border-color:#ccc;\">基本信息</td>\r\n");
                    sb.Append("  </tr>\r\n");
                    sb.Append("  <tr>\r\n");
                    sb.Append("    <td style=\"border-color:#ccc;\">课程名称：</td>\r\n");
                    sb.Append("    <td style=\"border-color:#ccc;\">" + model.CourseName + "</td>\r\n");
                    sb.Append("  </tr>\r\n");
                    sb.Append("  <tr>\r\n");
                    sb.Append("    <td style=\"border-color:#ccc;\">班级ID：</td>\r\n");
                    sb.Append("    <td style=\"border-color:#ccc;\">" + model.ClassID + "</td>\r\n");
                    sb.Append("  </tr>\r\n");
                    sb.Append("  <tr>\r\n");
                    sb.Append("    <td style=\"border-color:#ccc;\">班级编号：</td>\r\n");
                    sb.Append("    <td style=\"border-color:#ccc;\">" + model.ClassCode + "</td>\r\n");
                    sb.Append("  </tr>\r\n");
                    sb.Append("  <tr>\r\n");
                    sb.Append("    <td style=\"border-color:#ccc;\">班级人数：</td>\r\n");
                    sb.Append("    <td style=\"border-color:#ccc;\">" + model.CurrentCount + "</td>\r\n");
                    sb.Append("  </tr>\r\n");
                    sb.Append("  <tr>\r\n");
                    sb.Append("    <td style=\"border-color:#ccc;\">参与人数：</td>\r\n");
                    sb.Append("    <td style=\"border-color:#ccc;\">" + model.VoteList.Count + "</td>\r\n");
                    sb.Append("  </tr>\r\n");

                    IList<string[]> ratingList = new List<string[]>();
                    IList<string[]> overrallRatingList = new List<string[]>();
                    int i = 0;
                    int j = 0;

                    if (model.VoteList.Count > 0)
                    {
                        foreach (KeyValuePair<string, string> vote in model.VoteList)
                        {
                            ratingList.Add(vote.Key.IndexOf(",") != -1 ? vote.Key.Split(',') : new string[] { vote.Key });
                            overrallRatingList.Add(vote.Value.IndexOf(",") != -1 ? vote.Value.Split(',') : new string[] { vote.Value });
                        }
                    }

                    foreach (SurveyCategoryAndOptionModel optionModel in model.OptionList)
                    {
                        sb.Append("  <tr>\r\n");
                        sb.Append("    <td colspan=\"2\" style=\"font-weight:bolder; border-color:#ccc;\">" + optionModel.CategoryName + "</td>\r\n");
                        sb.Append("  </tr>\r\n");
                        sb.Append("  <tr>\r\n");
                        sb.Append("    <td style=\"border-color:#ccc;\">内容</td>\r\n");
                        sb.Append("    <td style=\"border-color:#ccc;\">评分</td>\r\n");
                        sb.Append("  </tr>\r\n");

                        foreach (KeyValuePair<int, string> kvp in optionModel.OptionList)
                        {
                            int total = 0;
                            decimal avg = 0;

                            if (ratingList.Count > 0)
                            {
                                foreach (string[] s in ratingList)
                                {
                                    total += Convert.ToInt32(s[i]);
                                }
                                avg = Math.Round((decimal)total / ratingList.Count, 2);
                            }

                            sb.Append("  <tr>\r\n");
                            sb.Append("    <td style=\"border-color:#ccc;\">" + kvp.Value + "</td>\r\n");
                            sb.Append("    <td style=\"border-color:#ccc;\">" + avg + "</td>\r\n");
                            sb.Append("  </tr>\r\n");
                            i++;
                        }

                        int overrallTotal = 0;
                        decimal overrallAvg = 0;

                        if (overrallRatingList.Count > 0)
                        {
                            foreach (string[] ts in overrallRatingList)
                            {
                                overrallTotal += Convert.ToInt32(ts[j]);
                            }
                            overrallAvg = Math.Round((decimal)overrallTotal / overrallRatingList.Count, 2);
                        }

                        sb.Append("  <tr>\r\n");
                        sb.Append("    <td style=\"border-color:#ccc;\">总体评价</td>\r\n");
                        sb.Append("    <td style=\"border-color:#ccc;\">" + overrallAvg + "</td>\r\n");
                        sb.Append("  </tr>\r\n");

                        j++;
                    }

                    sb.Append("</table><p>&nbsp;</p>\r\n");

                }
            }

            return sb;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
